<?php
//To unset the session variable if present
//including header.php 
include '../../include/header.php';
//including page.php (It is a class which contain functions)
include '../../libraries/page.php';
//created object for page.php
$page = new page();
session_start();
unset($_SESSION['display']);
$userid = $_SESSION['userid'];

//To get current date and time
date_default_timezone_set('Asia/Calcutta');
$date1 = date("Y-m-d H:i:s");

// checking the session
if (!$_SESSION['username']) {
    header("location:../user/login.php");
}
if ($_GET['status'] == '1') {
    $msg = $message[13];
} else if ($_GET['status'] == '0') {
    $msg = $message[0];
}
if ($_GET['q'] == 'logout') {
    $page->user_logout();
    header("location:../user/login.php");
}
if (isset($_POST['add'])) {
    if (isset($_POST['productid']) && isset($_POST['qty'])) { # Find out if the form had been submitted
        session_start();

        if (isset($_POST['display'])) {
            $display = unserialize(base64_decode($_POST['display']));
            array_push($display, array("productid" => $_POST['productid'], "qty" => $_POST['qty'], "discount" => $_POST['discount']));
        } else {
            $display = array();
            //$display[0] = $_POST;

            $display[0] = array("productid" => $_POST['productid'], "qty" => $_POST['qty'], "discount" => $_POST['discount']);
            //print_r($display);
        }
//$total_vat=$_POST['vat'];
    }
}
if (isset($_POST['delete'])) {

    if (isset($_POST['chkDel'])) {
        $chk = $_POST['chkDel'];
        for ($i = 0; $i < count($chk); $i++) {
            if ($chk[$i] != "") {
                //php code to get the data from groups table 
                if (isset($_POST['display'])) {
                    $display = unserialize(base64_decode($_POST['display']));
                    unset($display[$chk[$i]]);
                    Sort($display);
                    $msg = $message[12];
                } else {
                    $msg = $message[0];
                }
            }
        }
    }
}


$result = $display;
$dsc = mysql_query("select * from options");
$dsc_data = mysql_fetch_array($dsc);
for ($row = 0; $row < count($result); $row++) {
    // echo "  Name :" . $result[$row]['productid'] . " <br> qty :" . $result[$row]['qty'];
    // echo "<br />";

    $product_id = $result[$row]['productid'];
    $discount = $result[$row]['discount'];

    $products = mysql_query("select * from products where productid = $product_id");
    $products_data = mysql_fetch_array($products);

    if ($dsc_data['discount_type'] == "amount") {
        $total = ($products_data['price'] - $discount) * $result[$row]['qty'];
        $dup_total = $total_rate + $total;
        $total_rate = $dup_total;
        $vat_tax = ($total / 100) * 14.5;
        $amount = $total + $vat_tax;
        $dup_vat = $total_vat + $vat_tax;
        $total_vat = $dup_vat;
        $bill_total = $total_rate + $total_vat;
        $dup_discount = $total_discount + ($discount * $result[$row]['qty']);
        $total_discount = $dup_discount;
    } else {
        $total = ($products_data['price'] - ((($products_data['price']) / 100) * $discount)) * $result[$row]['qty'];
        $dup_total = $total_rate + $total;
        $total_rate = $dup_total;
        $vat_tax = ($total / 100) * 14.5;
        $amount = $total + $vat_tax;
        $dup_vat = $total_vat + $vat_tax;
        $total_vat = $dup_vat;
        $bill_total = $total_rate + $total_vat;
        $dup_discount = $total_discount + (((($products_data['price']) / 100) * $discount) * $result[$row]['qty']);
        $total_discount = $dup_discount;
    }
}
?>

<!------------------css style sheets for responsive tables------------------------------------>
<link rel="stylesheet" type="text/css" href="../../css/add_sales_billing/style_table.css">
<link rel="stylesheet" type="text/css" href="../../css/add_sales_billing/responsive_table.css">
<link rel="stylesheet" type="text/css" href="../../css/sales.css">

<!-------------------------------------Navbar---------------------------------->
<?
$stock_register = 'active';
include '../../include/navbar.php';
?>

<!----------------------------------Display the result message of Addition--------------------->
<div  align="center"><? echo $msg; ?></div>


<script src="../../js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#form form').submit(function() {
            $.get('post2.php', $(this).serialize(), function(data) {
                $('#content').html(data);
                
  $('#abc').html("<?php print_r($_SESSION['display']); ?>");  
    $('#abc').append("chandusksdnwke");
            });
            return false;
        });
    });
</script>
<!---------------------------------form------------------------------------------------>
<div id="abc">
    <?php    print_r($_SESSION['display']); ?>
</div>
<form method="POST" name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <div class="maindiv3" >
        <div class="sales_leftdiv" style="clear:both">
            <label>Bill Date</label>
            <input class="input-xlarge" id="focusedInput" type="text"  name ="bill_date" value="<?= $date1; ?>">
        </div>
        <div class="sales_middiv"></div>
        <div class="sales_leftdiv">
            <label>Bill to</label>
            <select name="bill_to" style="width:290px" >
                <?
                $customers = mysql_query("select * from customers");
                $id = 0;
                while ($customers_data = mysql_fetch_array($customers)) {
                ?>
                <option value="<?= $customers_data['customerid']; ?>"><?= $customers_data['customer']; ?></option>
                <?
                }
                ?>
                <option value="0">Others</option>

            </select>
        </div>
        <div class="sales_middiv"></div>
        <div class="sales_leftdiv">
            <label>Total</label>
            <input class="input-xlarge" id="focusedInput" type="text" name ="total_rate" value="<?= $total_rate; ?>"   readonly>
        </div>

        <div class="sales_leftdiv">
            <label>Discount</label>
            <input class="input-xlarge" id="focusedInput" type="number" name ="total_discount" value= "<?= $total_discount; ?>"  readonly>
        </div>
        <div class="middiv"></div>
        <div class="sales_leftdiv">
            <label>VAT @14%</label>
            <input class="input-xlarge" id="focusedInput" type="text" name ="total_vat" value="<?= $total_vat; ?>"  readonly>
        </div>
        <div class="middiv"></div>
        <div class="sales_leftdiv">
            <label>Bill Total</label>
            <input class="input-xlarge" id="focusedInput" type="text" name ="bill_total" value="<?= $bill_total; ?>" readonly >

        </div>


    </div>
</form>


<div id="form">
    <form>
        <div class="maindiv3" >
            <h3 style="color:#777;text-align: center; font-size: 18px">Billing</h3>
            <div class="sales_leftdiv">
                <label>Product</label>
                <select name="productid" style="width:290px" required>
                    <option></option>
                    <?
                    $products = mysql_query("select * from products");
                    $id = 0;
                    while ($products_data = mysql_fetch_array($products)) {
                    ?>
                    <option value="<?= $products_data['productid']; ?>"><?= $products_data['name']; ?></option>
                    <?
                    }
                    ?>
                </select>
            </div>
            <div class="sales_middiv"></div>
            <div class="sales_leftdiv">
                <label>Qty</label>
                <input class="input-xlarge" id="focusedInput" type="number"  name ="qty" value="1" >

<!--<input type="hidden" name="vat" value="<?//=$total_vat; ?>">-->
            </div>
            <div class="sales_leftdiv">
                <label>Discount</label>
                <input class="input-xlarge" id="focusedInput" type="number"  name ="discount" value="0" >
            </div>
            <div class="sales_rightdiv">
                <div class="btn1"><button class="btn btn-inverse"  type="submit" name="add" >Add</button></div>
            </div>
            <div id="content">

            </div>

        </div>
    </form>
</div>


<?php
//including footer.php
include '../../include/footer.php';
?>

